package com.xian.hander;

import com.xian.service.RpcRequest;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;

import java.util.concurrent.Callable;

public class UserClientHandler extends ChannelInboundHandlerAdapter implements Callable {

    //定义成员变量
    private  ChannelHandlerContext context;//存储  信息，写操作
    private String result;  //记录服务器返回数据
    private RpcRequest para;; //记录发送给服务器的数据
    //实现channleActive 客户端和服务器连接时，该方法自动执行
    @Override
    public void channelActive(ChannelHandlerContext channelHandlerContext) throws Exception {
            //初始化channelHandlerContext
        this.context = channelHandlerContext;
    }

    @Override
    public synchronized void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        //将读到的服务器数据，设置为成员变量
        result = msg.toString();
        notify();

    }

    public synchronized  Object call() throws Exception {
        context.writeAndFlush(para);
        wait();
        return result;
    }
    /*
     设置参数
     */
    public void setPara(RpcRequest para) {
        this.para = para;
    }
}
